﻿using System;
using System.Collections.Generic;
using System.Linq;
using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Enums;
using SelfMadeMillionaire.Core.Repositories;
using SelfMadeMillionaire.Core.Services;

namespace SelfMadeMillionaire.BLL.Services
{
	public class SchedulerService : ISchedulerService
	{
		private ISchedulerRepository _schedulerRepository;

		public SchedulerService(ISchedulerRepository schedulerRepository)
		{
			_schedulerRepository = schedulerRepository;
		}

		

		public void PrepareSchedule(EmailMessage message, NotificationType notificationType, int notificationTypeId)
		{
			IEnumerable<Scheduler> schedulers = _schedulerRepository.GetAll().Where(x =>
			x.AccessLevel >= message.MinAccessLevel &&
		x.NotificationType == (byte)notificationType
			&& x.TypeId == notificationTypeId
			);

			message.Schedules =
				schedulers.Select(scheduler => new EmailMessageSchedule
				{
					Scheduler = scheduler,
					AccessLevel = scheduler.AccessLevel,
					DateSent = DateTime.Now.AddDays(scheduler.OffsetInDays),
					Email = message
				}).ToArray();
		}
	}
}
